let currentPage = 1;
let Tid = null;
let totalPageCount = 0;
let s = encodeURI(window.location.href);
let strings = s.split("?currentPage=");
if (strings[1] != null) {
    currentPage = strings[1].split("&")[0];
}
if (s.indexOf("Tid")) {
    Tid = s.split("&Tid=")[1];
}
$(function () {
    $.ajax({
        url: path+"/blog/getBlogBypage",
        type: "GET",
        data: {"pageNum": currentPage, "Tid": Tid},
        success: function (data) {
            for (let i = 0; i < data.length; i++) {
                $("#context").append("" +
                    "<div class='cl-card'>" +
                    "    <div class='row'>" +
                    "    <div class='col-lg-3 col-md-4 col-sm-12 cl-card-image'><a href='calmlog-details.jsp?pid=" + data[i].id + "'><img src='temporary-img/list-image2.jpg' class='img-fluid ' alt=''></a></div>" +
                    "<div class='col-lg-9 col-md-8 col-sm-12 cl-card-main'>" +
                    "    <h3 class='cl-card-main-title'><a href='calmlog-details.jsp?pid=" + data[i].id + "'>" + data[i].title + "</a></h3>\n" +
                    "<p class='cl-card-main-info'>" + data[i].introduction + "</p>" +
                    "<p class='cl-card-more'><a href='calmlog-details.jsp?pid=" + data[i].id + "'>阅读更多...</a></p>\n" +
                    "</div>" +
                    "</div>" +
                    "</div>")
            }
        }
    })
    $.ajax({
        url: path+"/blog/getBlogNum",
        type: "GET",
        data: {"Tid": Tid},
        async: false,
        success: function (data) {
            totalPageCount = data.pageCount;
        }
    })
})


function getParameter(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    let r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

//init
window.onload = function () {
    let s = encodeURI(window.location.href);
    let strings = s.split("?currentPage=");

    let totalPage = totalPageCount;
    let pageNo = getParameter('pno');
    if (!pageNo) {
        if (strings[1] != null) {
            pageNo = strings[1].split("&")[0];
        } else {
            pageNo = 1;
        }
    }
    //生成分页
    //有些参数是可选的，比如lang，若不传有默认值
    kkpager.generPageHtml({
        //当前页码
        pno: pageNo,
        //总页码
        total: totalPage,
        mode: 'click',//默认值是link，可选link或者click
        click: function (n) {
            this.selectPage(n);
            if (n == 1) {
                window.location.href = this.getHref(n);
            } else {
                //除了第一页写逻辑跳转
                window.location.href = this.getHref(n);
            }
            return false;
        }
    });
};
